library(cmaes)

f1 <- function(x){
	return(abs(-3*x[1] + 2*x[2]**2 - 7 ))
}

x0 <- c(-10, 10)
control = list()
control["sigma"] = 10
control["lambda"] = 100
control["diag.sigma"] = TRUE
control["diag.pop"] = TRUE
control["diag.value"] = TRUE
resultat <- cma_es(x0, f1, control = control)
sol <- resultat$par
print("Solution :")
print(sol)
print(paste("Valeur de f :", f1(sol)))

details <- resultat$diagnostic
print(paste("Nombre d'iterations :", length(details$sigma)))


library(lattice)
nbRow <- 300
nbCol <- 300
stepX1 <- 0.1
stepX2 <- 0.1
maxX1 <- stepX1*nbRow/2
minX1 <- -maxX1
maxX2 <- stepX2*nbCol/2
minX2 <- -maxX2
x1_values <- seq(minX1, maxX1, stepX1)[1:nbRow]
x2_values <- seq(minX2, maxX2, stepX2)[1:nbCol]

dataCarto = matrix(0, nrow = nbRow, ncol = nbCol)
for(i in 1:nbRow){
	for(j in 1:nbCol){
		x1 <- x1_values[i]
		x2 <- x2_values[j]
		x <- c(x1, x2)
		dataCarto[i, j] = f1(x)
	}
}

library(grid)
fPoints = function(...){
	panel.levelplot(col = 2, ...)
	#grid.points(runif(10), runif(10), pch = 5)
	#legend("bottomright", legend = c("iteration 1", "iteration 2"), col = c(col1, col2), pch = 8)
}
#par(mfrow = c(2,1))
#plot(levelplot(dataCarto, main = "f(x, y) = |-3*x + 2*y² - 7 |", xlab = "x1", ylab ="x2", las=1, row.values = x1_values, column.values = x2_values))

col1 <- 2
col2 <- 3
col3 <- 4
colEvoPath <- 9
pchPoints <- 3

#library("RColorBrewer")
#brewer.div <-
    #colorRampPalette(brewer.pal(50, "Spectral"),
                     #interpolate = "spline")

plot(levelplot(dataCarto, main = "f(x, y) = |-3*x + 2*y² - 7 |", xlab = "x1", ylab ="x2", las=1, row.values = x1_values, column.values = x2_values, panel = fPoints,# col.regions = brewer.div(500),
	key = list(space = "bottom",
		lines = list(type = c("p", "p", "p", "p", "p", "p", "l"),
		col = c(col1, col2, col3, col1, col2, col3, colEvoPath),
			pch = c(pchPoints, pchPoints, pchPoints, 8, 8, 8),
			#lty = c(-1, -1, -1, -1, 1),
			cex = c(0.5, 0.5, 0.5, 1.5, 1.5, 1.5)
		),text  = list(c("Individus de l'itération 1", "Individus de l'itération 10", "Individus de l'itération 20", "Moyenne de l'itération 1", "Moyenne de l'itération 10", "Moyenne de l'itération 20", "Chemin d'évolution")))))

trellis.focus("panel", 1, 1, highlight=FALSE)
ind1 <- 1
ind2 <- 10
ind3 <- 20
indEvoPath <- 3
points1 <- t(details$pop[,,ind1])
points2 <- t(details$pop[,,ind2])
points3 <- t(details$pop[,,ind3])
lpoints(points1[,1], points1[,2], pch=pchPoints, col=col1, cex=1)
lpoints(points2[,1], points2[,2], pch=pchPoints, col=col2, cex=1)
lpoints(points3[,1], points3[,2], pch=pchPoints, col=col3, cex=1)
m1 <- colMeans(points1)
m2 <- colMeans(points2)
m3 <- colMeans(points3)
lpoints(m1[1], m1[2], pch=8, col=col1, cex=3)
lpoints(m2[1], m2[2], pch=8, col="darkgreen", cex=3)
lpoints(m3[1], m3[2], pch=8, col="darkblue", cex=3)
evoPath <- c(0,0)
for (i in 2:(indEvoPath)){
	points_old <- t(details$pop[,,i-1])
	points <- t(details$pop[,,i])
	m_old <- colMeans(points_old)
	m <- colMeans(points)
	s <- details$sigma[i]
	evoPath <- evoPath + (m-m_old)/s
}
origEvoPath <- c(-5,2) #TOCHANGE
larrows(origEvoPath[1], origEvoPath[2], origEvoPath[1]+evoPath[1], origEvoPath[2]+evoPath[2], col=colEvoPath, length = 0.1)
#larrows(0, 0, m2[1], m2[2], col=col2)
trellis.unfocus()
